package switchviz.graphic.gauge;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.jdom.Element;

public class GaugeDialog extends JDialog implements WindowListener
{
	/**
	 * 
	 */
	public static final String APPLY = "applyConfSetup";
	public static final String CANCEL = "cancelConfSetup";
	private static final long serialVersionUID = 7512971008775788702L;
	private JTabbedPane _tabpane = null;
	private List<JPanel> _panellist = null;
	private JButton _buttapply = null;
	private JButton _buttcancel = null;
	private int _index = -1;
	private IGauge _gauge = null;
	private static GaugeDialog _instance = null;
	
	private void setPanelList(List<JPanel> panellist)
	{
		_panellist = panellist;
	}
	
	public void setGauge(IGauge gauge)
	{
		_gauge = gauge;
		this.setPanelList(gauge.getViewGaugePanel());
		initialize();
	}
	
	public void setIndex(int index)
	{
		this._index = index;
	}
	
	public int getIndex()
	{
		return _index;
	}
	
	public static GaugeDialog getInstance()
	{
		if(_instance != null)
			_instance.setVisible(false);
		_instance = new GaugeDialog();
		return _instance;
	}
	
	private GaugeDialog()
	{
		this.setSize(new Dimension(500, 500));
		this.setVisible(true);
	}
	
	public Element getElement()
	{
		return _gauge.getElement();
	}
	
	private void initialize()
	{
		_tabpane = new JTabbedPane();
		Iterator<JPanel> it = _panellist.iterator();
		while(it.hasNext())
		{
			JPanel tab = it.next();
			_tabpane.addTab(tab.getName(), tab);
		}
		
		JPanel buttpanel = new JPanel();
		_buttapply = new JButton("Apply");
		_buttapply.setActionCommand(APPLY);
		
		_buttcancel = new JButton("Cancel");
		_buttcancel.setActionCommand(CANCEL);
		
		buttpanel.setLayout(new BoxLayout(buttpanel, BoxLayout.LINE_AXIS));
		buttpanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		buttpanel.add(_buttcancel);
		buttpanel.add(_buttapply);
		
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(_tabpane, BorderLayout.CENTER);
		getContentPane().add(buttpanel, BorderLayout.SOUTH);
		this.addWindowListener(this);
	}
	
	public void addActionListener(ActionListener l)
	{
		_buttapply.addActionListener(l);
		_buttcancel.addActionListener(l);
	}

	public void windowClosed(WindowEvent evt) 
	{
		_buttcancel.doClick();
	}

	public void windowClosing(WindowEvent evt) 
	{
		_buttcancel.doClick();
	}


	public void windowDeactivated(WindowEvent arg0) {}
	public void windowDeiconified(WindowEvent arg0) {}
	public void windowIconified(WindowEvent arg0) {}
	public void windowOpened(WindowEvent arg0) {}
	public void windowActivated(WindowEvent arg0) {}
}
